package com.xy.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Properties;
@Component
@Slf4j
public class PropertiesUtil {
    private static Properties props = null;
    private void init (){
        if(props==null){
            props = new Properties();
        }
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("application.properties")), StandardCharsets.UTF_8);
            props.load(inputStreamReader);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    public static String getValue(String key,String defaultValue){
        if (props==null){
            new PropertiesUtil().init();
        }
        String value = props.getProperty(key);
        if(value==null){
            return defaultValue;
        }else {
            return value;
        }
    }
}
